#include "Main.h"
#include <iostream>
using namespace stalemateMain;

Glib::RefPtr<Gtk::Builder> builder;
Glib::ustring gladeDir = "./gui/stalemate.glade";
string stalemateMain::fleetDir;
string stalemateMain::mapDir;
string stalemateMain::saveDir;
//entry point------------------------------------
int main(int argc, char* argv[]) {
//    Gtk::Main kit = Gtk::Main(argc, argv);

    //load up the data file
    fstream dfile(dataFile);
    string tok;

    while (dfile.good()) {
        dfile >> tok;
        if (tok.compare("FLT") == 0) {
            dfile >> stalemateMain::fleetDir;
            cout << "Loading fleet dir: " << stalemateMain::fleetDir << endl;
        } else if (tok.compare("MAPS") == 0) {
            dfile >> stalemateMain::mapDir;
            cout << "Loading map dir:  " << stalemateMain::mapDir << endl;
        } else if (tok.compare("SAVE") == 0) {
            dfile >> stalemateMain::saveDir;
            cout << "Loading save dir:  " << stalemateMain::saveDir << endl;
        }
    }
    dfile.close();
/*
    builder = Gtk::Builder::create_from_file(gladeDir);
    stalemateMain::controll* stalemate = stalemateMain::controll::gsControll();
    stalemate->set_visible(true);
    Gtk::Main::run(*stalemate);
 */
}
//===============================================

//main screen++++++++++++++++++++++++++++++++++++
// the main window thats responsible for loading
// all the other screens
//-----------------------------------------------
//static vars------
stalemateMain::controll* stalemateMain::controll::screen;
//-----------------
//constructors-----
stalemateMain::controll::controll(BaseObjectType* obj, const Glib::RefPtr<Gtk::Builder>& bdr) : Gtk::Window(obj) {
    //comp pointers
    active = NULL;
    box = NULL;
    //load comps
    builder->get_widget(TBOX, box);
    //setup the mutexs and libraries
    mrep = new mapdata::maprep();
    players = new Players::playerLib();
    pthread_mutex_init(mrepMute, NULL);
    pthread_mutex_init(playerMute, NULL);
    //remember first screen
    remember(introScreens::introScr::gsIntroScr());
}
stalemateMain::controll::~controll() {
    pthread_mutex_destroy(mrepMute);
    pthread_mutex_destroy(playerMute);
}
stalemateMain::controll* stalemateMain::controll::gsControll() {
    if (screen == NULL) {
        builder->get_widget_derived(MainWin, screen);
    }
    return screen;
}
//-----------------
//mem--------------
void stalemateMain::controll::remember(Gtk::Widget* scr) {
    if (scr != NULL && active != scr) {
        if (active != NULL) {
            box->remove(*active);
        }
        scr->set_visible(true);
        box->add(*scr);
        active = scr;
    }
}
//gloabalish-------
mapdata::maprep* controll::gGameMap() {
    pthread_mutex_lock(mrepMute);
    return mrep;
}
void controll::rGameMap() {
    pthread_mutex_unlock(mrepMute);
}
Players::playerLib* controll::gPlayers() {
    pthread_mutex_lock(playerMute);
    return players;
}
void controll::rPlayers() {
    pthread_mutex_unlock(playerMute);
}
//===============================================
